package com.example.Udp_socket;

        import java.net.DatagramPacket;
        import java.net.DatagramSocket;
        import java.net.InetAddress;

        import android.os.Bundle;
        import android.os.Message;
        import android.app.Activity;
        import android.view.Menu;
        import android.view.View;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.TextView;
        import android.os.Handler;

public class MyActivity extends Activity implements
        android.view.View.OnClickListener {
    public static final String SERVERIP = "10.95.58.200";
    public static final int SERVERPORT = 60000;
    public TextView text1;
    public EditText input;
    public Button btn;
    public boolean start;
    public Handler Handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        text1 = (TextView) findViewById(R.id.textView1);
        input = (EditText) findViewById(R.id.editText1);
        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(this);
        start = false;
        new Thread(new Server()).start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {

        }
        new Thread(new Client()).start();
        Handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                String text = (String) msg.obj;
                text1.append(text);
            }
        };
    }

    public class Client implements Runnable {
        @Override
        public void run() {
            while (start == false) {
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                InetAddress serverAddr = InetAddress.getByName(SERVERIP);
                updatetrack("Client: Start connecting\n");
                DatagramSocket socket = new DatagramSocket();
                byte[] buf;
                if (!input.getText().toString().isEmpty()) {
                    buf = input.getText().toString().getBytes();
                } else {
                    buf = ("Default message").getBytes();
                }
                DatagramPacket packet = new DatagramPacket(buf, buf.length,
                        serverAddr, SERVERPORT);
                updatetrack("Client: Sending ‘" + new String(buf) + "’\n");
                socket.send(packet);
                updatetrack("Client: Message sent\n");
                updatetrack("Client: Succeed!\n");
            } catch (Exception e) {
                updatetrack("Client: Error!\n");
            }
        }
    }

    public class Server implements Runnable {
        @Override
        public void run() {
            while (start == false) {
            }
            try {
                InetAddress serverAddr = InetAddress.getByName(SERVERIP);
                updatetrack("\nServer: Start connecting\n");
                DatagramSocket socket = new DatagramSocket(SERVERPORT,
                        serverAddr);
                byte[] buf = new byte[17];
                DatagramPacket packet = new DatagramPacket(buf, buf.length);
                updatetrack("Server: Receiving\n");
                socket.receive(packet);
                updatetrack("Server: Message received: ‘"
                        + new String(packet.getData()) + "’\n");
                updatetrack("Server: Succeed!\n");
            } catch (Exception e) {
                updatetrack("Server: Error!\n");
            }
        }
    }

    public void updatetrack(String s) {
        Message msg = new Message();
        String textTochange = s;
        msg.obj = textTochange;
        Handler.sendMessage(msg);
    }

/*
    @Override

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.layout.main, menu);
        return true;
    }
*/
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v.getId()==R.id.button1)
            start = true;
    }
}
